<?php
require_once('../../libraryfiles/config.php');
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {

    case 'add_promo' :
        addPromotion();
        break;
    case 'edit_promo' :
        editPromotion();
        break;
    default :
        header('Location: index.php');
}

//this is function for adding advertisement 
function addPromotion() {
    if (isset($_POST["btnsave"])) {
        $product_id = ampReplace($_POST['selectProduct']);
        //$phar_id = mysql_query("SELECT phar.pharmacy_id FROM tblpharmacy AS phar,tblproduct AS p WHERE phar.user_id = p.owner AND p.id =$product_id");
        //$pharmacy_id = ampReplace($phar_id);
        $quantity = ampReplace($_POST['txtQuantity']);
        $promotion = ampReplace($_POST['txtPromotion']);
        $Date_stock = ampReplace($_POST['txtDateStock']);
        $strInsert = "INSERT INTO promotion(
                                quantity,
                                date_in_stock,
                                promotion,
                                product_id                                                        
                )	
                VALUES(
                           '" . $quantity . "',
                           '" . $Date_stock . "',
                           '" . $promotion . "',
                           '" . $product_id . "'                                                   
                           )";

        mysql_query($strInsert) or die('Error with query insert.' . mysql_error());

        header("location:index.php?view=promotion");
    }

    header('Location: index.php?view=promotion');
}

//this is function for edit advertisement 

function editPromotion() {
    if (isset($_POST["btnsave"])) {
        $product_id = ampReplace($_POST['selectProduct']);
       // $phar_id = mysql_query("SELECT phar.pharmacy_id FROM tblpharmacy AS phar,tblproduct AS p WHERE phar.user_id = p.owner AND p.id =$product_id");
        //$pharmacy_id = ampReplace($phar_id);
        $quantity = ampReplace($_POST['txtQuantity']);
        $promotion = ampReplace($_POST['txtPromotion']);
        $Date_stock = ampReplace($_POST['txtDateStock']);
       
        if (isset($_SESSION['id_for'])) {
            $ids = $_SESSION['id_for'];
        }        
                                                          
            $strUpdate = "UPDATE promotion SET 
                        quantity='" . $quantity . "',
			date_in_stock = '" . $Date_stock . "',
			promotion='" . $promotion . "',
			product_id = '" . $product_id . "'								
			WHERE promotion_id = '" . $ids . "'";
            mysql_query($strUpdate) or die('Error with query update.' . mysql_error());
       
          
    }

    header('Location: index.php?view=promotion');
}

?>